/*
 * Copyright (c)  2017, ueboot.com
 * All rights reserved.
 * 2017-10-14 21:28:34
 */
package com.ueboot.weishop.api.controller.goods;

import com.alibaba.fastjson.JSON;
import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.api.controller.goods.vo.GoodsInfoResp;
import com.ueboot.weishop.entity.activity.ActivityEntity;
import com.ueboot.weishop.entity.goods.GoodsInfoEntity;
import com.ueboot.weishop.service.activity.ActivityService;
import com.ueboot.weishop.service.goods.GoodsInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by Neel  on 2017-10-14 21:28:34.
 *
 * @author Neel
 * @since 2.0 by autocode
 */
@Slf4j
@RestController
@RequestMapping(value = "/api/public/goodsInfo/")
public class GoodsInfoController {


	@Autowired
	private GoodsInfoService goodsInfoService;

	@RequestMapping(value = "categories/{code}/goods", method = RequestMethod.GET)
	public Response<List<GoodsInfoResp>> list(@PathVariable("code") String code) {
		List<GoodsInfoEntity> entities = goodsInfoService.findByCode(code);

		List<GoodsInfoResp> body = new ArrayList<>();
		for (GoodsInfoEntity entity : entities) {
			GoodsInfoResp resp = new GoodsInfoResp();
			BeanUtils.copyProperties(entity, resp);
			if (entity.getMinPrice() != null) {
				resp.setMinPrice(entity.getMinPrice() / 100);
			}
			ActivityEntity activity = entity.getActivity();
			if(activity==null){
				continue;
			}
			if ( activity.getEndTime() != null && new Date().after(activity.getEndTime())) {
				continue;
			}
			resp.setSn(activity.getSn());
			if (ActivityEntity.STOCK_COUNT_TYPE_ACT.equals(activity.getStockCountType()) && ActivityEntity.TYPE_DISCOUNT.equals(activity.getType())) {
				resp.setDiscountValue(activity.getDiscountValue());
				resp.setLimitCopies(activity.getLimitCopies());
				resp.setDiscountOriginPrice(activity.getDiscountOriginPrice());
			}
			body.add(resp);
		}

		return new Response<>(body);
	}

	@RequestMapping(value = "get/{id}", method = RequestMethod.GET)
	public Response<GoodsInfoResp> get(@PathVariable Long id) {
		String goodsInfo = goodsInfoService.findById(id);
		GoodsInfoEntity entity = JSON.parseObject(goodsInfo, GoodsInfoEntity.class);
		GoodsInfoResp resp = new GoodsInfoResp();
		BeanUtils.copyProperties(entity, resp);
		resp.setCategoryCode(entity.getCategory().getCode());
		ActivityEntity activity = entity.getActivity();
		resp.setSn(activity.getSn());
		if (ActivityEntity.STOCK_COUNT_TYPE_ACT.equals(activity.getStockCountType()) && ActivityEntity.TYPE_DISCOUNT.equals(activity.getType())) {
			resp.setDiscountValue(activity.getDiscountValue());
			resp.setLimitCopies(activity.getLimitCopies());
			resp.setDiscountOriginPrice(activity.getDiscountOriginPrice());
		}
		return new Response<>(resp);
	}

}
